#include "Game.h"
#include "GameState.h"
#include "utilities.h"
#include "..\..\source\Shaders.h"
#include "..\..\source\Vertex.h"
class GameTest : public Game
{
public:
	GameTest(){}
	void init() {}
	void destroy(){}
};

class StateTest : public GameState
{
public:
	GLuint vboId;
	Shaders myShaders;
	void init()
	{

		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

		//triangle data (heap)
		Vertex verticesData[3];

		verticesData[0].pos.x = 0.0f;  verticesData[0].pos.y = 0.5f;  verticesData[0].pos.z = 0.0f;
		verticesData[1].pos.x = -0.5f;  verticesData[1].pos.y = -0.5f;  verticesData[1].pos.z = 0.0f;
		verticesData[2].pos.x = 0.5f;  verticesData[2].pos.y = -0.5f;  verticesData[2].pos.z = 0.0f;

		//buffer object
		glGenBuffers(1, &vboId);
		glBindBuffer(GL_ARRAY_BUFFER, vboId);
		glBufferData(GL_ARRAY_BUFFER, sizeof(verticesData), verticesData, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);

		//creation of shaders and program 
		myShaders.Init("../../data/shaders/TriangleShaderVS.vs", "../../data/shaders/TriangleShaderFS.fs");
	}
	void render()
	{
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(myShaders.program);

		glBindBuffer(GL_ARRAY_BUFFER, vboId);


		if (myShaders.positionAttribute != -1)
		{
			glEnableVertexAttribArray(myShaders.positionAttribute);
			glVertexAttribPointer(myShaders.positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
		}

		glDrawArrays(GL_TRIANGLES, 0, 3);

		glBindBuffer(GL_ARRAY_BUFFER, 0);

	}
	void update(float dt)
	{ 

	}
	void exit(){}
};


Game* game;
GameState* state;

int Init(ESContext *esContext)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	game = new GameTest;
	state = new StateTest;
	StateMangager::getInstance()->switchState(state);
	game->resume();
	return 0;
}

void Draw(ESContext *esContext)
{
	glClear(GL_COLOR_BUFFER_BIT);

	game->render();
	eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}

void Update(ESContext *esContext, float deltaTime)
{
	game->update(deltaTime);
}

void Key(ESContext *esContext, unsigned char key, bool bIsPressed)
{

}

void CleanUp()
{
}


#include "SoundManager.h"

int main()
{

	
	return 0;
	//identifying memory leaks
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	ESContext esContext;

	esInitContext(&esContext);

	esCreateWindow(&esContext, "Hello Triangle", DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, ES_WINDOW_RGB | ES_WINDOW_DEPTH);

	if (Init(&esContext) != 0)
		return 0;
	
	esRegisterDrawFunc(&esContext, Draw);
	esRegisterUpdateFunc(&esContext, Update);
	esRegisterKeyFunc(&esContext, Key);

	esMainLoop(&esContext);

	//releasing OpenGL resources
	CleanUp();


	return 0;
}

